//
//  SetPasswordView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI

struct AiCustomItemView: View {
    var title : String
    var key : String = IAiModelType.diaryReply.rawValue
    @State private var isShow = false
    @State private var host = ""
    @State private var token = ""
    @State private var model = ""

    var body: some View {
        HStack{
            VStack(alignment: .leading, spacing: 18) {
                HStack{
                    Text(title)
                        .foregroundColor(.bgItemContent)
                        .fontWeight(.bold)
                        .font(.system(size: 14))
                    Spacer()
                    Image(.myIconExpand)
                        .resizable()
                        .frame(width: 15,height: 15)
                        .rotationEffect(isShow ? Angle(degrees: 180) : .zero)
                }.contentShape(Rectangle())// 将整个矩形区域（包括padding）都设置为可点击区域
                    .onTapGesture {
                        if !userManager.isCustom() {
                            showToast("该功能需要定制会员才能使用")
                            return
                        }
                        withAnimation(Animation.easeInOut(duration: 0.3)) {
                            isShow.toggle()
                        }
                    }
                
                if isShow {
                    HStack {
                        Text("API地址")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        Spacer()
                        Text(host)
                            .foregroundColor(.bgNormalText4)
                            .font(.system(size: 10))
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            showEditDialogAllowEmpty(title: "API地址", hint: "请输入API地址", content: host) { it in
                                let bean = getAiModelCustom()
                                bean.host = it
                                AiManager.shared.saveAiCustom(key: key, bean: bean) {
                                    host = it
                                }
                            }
                        }
                    HStack {
                        Text("令牌")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        Spacer()
                        Text(token)
                            .foregroundColor(.bgNormalText4)
                            .font(.system(size: 10))
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            showEditDialogAllowEmpty(title: "令牌", hint: "请输入令牌", content: token) { it in
                                let bean = getAiModelCustom()
                                bean.token = it
                                AiManager.shared.saveAiCustom(key: key, bean: bean) {
                                    token = it
                                }
                            }
                        }
                    HStack {
                        Text("模型名称")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        Spacer()
                        Text(model)
                            .foregroundColor(.bgNormalText4)
                            .font(.system(size: 10))
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            showEditDialogAllowEmpty(title: "模型名称", hint: "请输入模型名称", content: model) { it in
                                let bean = getAiModelCustom()
                                bean.model = it
                                AiManager.shared.saveAiCustom(key: key, bean: bean) {
                                    model = it
                                }
                            }
                        }
                    HStack {
                        Text("状态")
                            .foregroundColor(.bgItemContent)
                            .font(.system(size: 14))
                        Spacer()
                        Text(isCompleted() ? "已完成" :"未完成")
                            .foregroundColor(.bgNormalText4)
                            .font(.system(size: 10))
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                           
                        }
                }
            }
            Spacer()
        }.frame(maxWidth: .infinity).padding(18).background(.bgItem).clipShape(RoundedRectangle(cornerRadius: 15))
            .onAppear{
                host = getAiModelCustom().host ?? ""
                token = getAiModelCustom().token ?? ""
                model = getAiModelCustom().model ?? ""
            }
    }
    
    func getAiModelCustom()-> AiModelCustomBean {
        let bean = AiManager.shared.getAiCustom(key: key)
        return bean
    }
    
    func isCompleted() -> Bool {
        return AiCustomUtils.shared.isCustomAi(type: key)
    }
}

//#Preview {
//    AiCustomItemView()
//}

//定义预览提供者
struct AiCustomItemView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: AiCustomItemView(title: "聊天"))
    }
}
